package it.lolled.core;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileUtils {
	String filename = "posts.txt";

	public static void main(String[] args) {
		(new FileUtils()).readFile();
	}

	public void checkExist() throws IOException {
		File f = new File(filename);
		if (!f.exists()) {
			f.createNewFile();
		}
	}

	public String readFile() {
		BufferedReader br = null;
		String content = "";

		try {
			checkExist();
			String sCurrentLine;
			br = new BufferedReader(new FileReader(filename));

			while ((sCurrentLine = br.readLine()) != null) {
				content += sCurrentLine;
			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (br != null) {
					br.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}

		return content;
	}

	public void appendFile(String content) {
		FileWriter f = null;

		try {
			checkExist();
			f = new FileWriter(filename);
			f.append(content);
			f.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (f != null) {
					f.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
	}
}
